package com.example.ying.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.ying.common.R;
import com.example.ying.mapper.MovieMapper;
import com.example.ying.mapper.RateMapper;
import com.example.ying.model.domain.Rate;
import com.example.ying.service.RateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RateServiceImpl extends ServiceImpl<RateMapper, Rate> implements RateService {

    @Autowired
    private RateMapper rateMapper;
    @Autowired
    private MovieMapper movieMapper;
    @Override
    public R addRate(Rate rate) {
        if (rateMapper.insert(rate) > 0) {
            return R.success("评价成功");
        } else {
            return R.error("评价失败");
        }
    }

    @Override
    public R deleteRate(Integer movieId, String userId) {
        if (rateMapper.deleteByMovieIdAndUserId(movieId, userId)) {
            return R.success("删除评分成功");
        } else {
            return R.error("删除评分失败");
        }
    }

    @Override
    public R getRateByMovieId(Integer movieId) {

        return R.success(null,movieMapper.selectRateBydoubanId(movieId));
    }

    @Override
    public R getRateByUserIdAndMovieId(String userId, Integer movieId) {
        Integer rate = rateMapper.selectRateByUserIdAndMovieId(userId, movieId);

        if (rate != null) {
            return R.success(null,rate);
        }else{
            return R.error("未找到该用户的评分");
        }


    }
}
